package com.blt.govdata.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.blt.govdata.controller.base.BaseController;
import com.blt.govdata.model.TProject;
import com.blt.govdata.service.ICommonService;
import com.blt.govdata.service.ITProjectService;
import com.blt.govdata.utils.ExcelUtil;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 * 定向监测 Excel前端控制器
 * </p>
 * @author AustinGJ
 * @since 2018-08-23
 */
@RestController
@RequestMapping("/excel")
public class ExcelController extends BaseController {

    @Autowired
    private ITProjectService projectService;

    @Autowired
    ICommonService commonService;
    @RequestMapping(value = "/exportExcelByIds")

    public void exportExcelByIds(@RequestParam(defaultValue = "0") String ids) {
        List<Integer> idss = Arrays.asList(ids.split(",")).stream().map(e->Integer.valueOf(e)).collect(Collectors.toList());
        EntityWrapper ew= new EntityWrapper();
        ew.setEntity(TProject.class);
        List<String> colunms = Lists.newArrayList();//列名
        List<Map<String,Object>> result = this.commonService.selectByIds(idss);
        List<Object[]> contentList = new ArrayList<>();
        if(!CollectionUtils.isEmpty(result)) {
            Map<String,Object> map = result.get(0);
            for(String key:map.keySet()){
                colunms.add(key);
            }
            for (Map<String,Object> row:result) {
                List<Object> values = new ArrayList<>();
                for (String key:row.keySet()){
                    values.add(row.get(key));
                }
                Object[] o = new Object[row.size()];
                values.toArray(o);
                contentList.add(o);
            }
            String[] headArray = new String[colunms.size()];
            colunms.toArray(headArray);
            try {
                SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd_hhMMss");
                String dateString = String.valueOf(sdf.format(new Date()));
                ExcelUtil.ExportExcel(response, headArray, contentList, "export_"+dateString+".xls");
            } catch (Exception e) {
                System.out.println(e.getMessage());
            }
        }else{
            return;
        }




    }


    @RequestMapping(value = "/exportExcelBySQL")

    public void exportExcelBySQL(@RequestParam(defaultValue = "SELECT '无' AS '数据' FROM DUAL") String sql) {
        sql = sql.toLowerCase();
        if(StringUtils.containsAny(sql,"delete","update","truncate")) {
            System.out.println("不支持此类操作");
            return;
        }
        EntityWrapper ew= new EntityWrapper();
        ew.setEntity(TProject.class);
        List<String> colunms = Lists.newArrayList();//列名
        List<Map<String,Object>> result = this.commonService.selectBySQL(sql);
        List<Object[]> contentList = new ArrayList<>();
        if(!CollectionUtils.isEmpty(result)) {
            Map<String,Object> map = result.get(0);
            for(String key:map.keySet()){
                colunms.add(key);
            }
            for (Map<String,Object> row:result) {
                List<Object> values = new ArrayList<>();
                for (String key:row.keySet()){
                    values.add(row.get(key));
                }
                Object[] o = new Object[row.size()];
                values.toArray(o);
                contentList.add(o);
            }
            String[] headArray = new String[colunms.size()];
            colunms.toArray(headArray);
            try {
                SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd_hhMMss");
                String dateString = String.valueOf(sdf.format(new Date()));
                ExcelUtil.ExportExcel(response, headArray, contentList, "export_"+dateString+".xls");
            } catch (Exception e) {
                System.out.println(e.getMessage());
            }
        }else{
            return;
        }
    }

}
